var  client = require('../modules/db_mysql');
var  uid = require('../modules/uuid');//用于生成id
function  User(user) {
    this.name = user.name;
    this.password = user.password;
}
var tableName = "user";
mysql = client.getDbCon();
module.exports = User;

//新增用户
User.prototype.save = function save(callback) {
    // 用户对象
    var  user = {
        login_name: this.login_name,
        display_name: this.display_name,
        password: this.password
    };
    uuid = uid.v4();
    //插入数据库
    var sql ="insert into user (id, login_name, display_name, password) values(?,?,?,?)";

    mysql.query(sql, [uuid, user.login_name, user.display_name, user.password], function(err,results,fields){
            connection.release();
        if (err) {
            throw err;
        } else {
            //返回用户id
            return callback(err, uuid, fields);
        }
    });
};

//获取用户
User.get = function get(username, callback) {

    // 读取 users 集合
    var sql = "select c.id, c.login_name, c.display_name, c.password from user c where c.login_name='"+username+"'";
    console.log(sql);
    mysql.query(sql,function(err, results, fields){
        connection.release();
        if(err){
            throw err;
        }else{
            console.log(results[0]);
            callback(err,results[0],fields);
        }
    })

};